<template>
  <div class="app-container">
    <el-card class="content-edit">
      <div slot="header">{{ pageType==='add'?'添加协议':'编辑协议' }}</div>
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="协议名称" maxlength="20">
          <el-input v-model="form['name']"/>
        </el-form-item>
        <el-form-item label="协议内容" maxlength="20">
          <ws-editor v-model="form['content']"/>
        </el-form-item>
        <el-form-item>
          <el-button :loading="loading" type="primary" @click="onSubmit">保存</el-button>
          <el-button @click="$router.back()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
  export default {
    name: 'RentCarAgreementModify',
    data() {
      return {
        pageType: 'add',
        loading: false,
        form: {
          // 路线名称
          name: '',
          // 协议内容
          content: ''
        }
      }
    },
    mounted(){
      this.form['id'] = this.$router.currentRoute.query['id'] || '';
      if (this.$router.currentRoute.path.indexOf('add') > 0) {
        this.pageType = 'add'
      } else {
        this.pageType = 'edit';
        this.loadData();
      }
    },
    methods: {
      // 加载信息
      loadData(){
        (async () => {
          const result = await this.$service.applicationGetRentCarAgreementDetails({id: this.form['id']});
          if (result.code === 0) {
            Object.assign(this.form, result.data);
          }
        })();
      },
      // 提交
      onSubmit() {
        this.$refs.form.validate(valid => {
          if (valid) {
            this.loading = true;
            (async () => {
              const params = Object.assign({}, this.form);
              let result = null;
              if (this.pageType === 'add') {
                result = await this.$service.applicationAddRentCarAgreement(params);
              } else {
                result = await this.$service.applicationModifyRentCarAgreement(params);
              }
              this.loading = false;
              if (result.code === 0) {
                this.$message.success('操作成功~');
                this.$router.back();
              } else {
                this.$message.error(result.msg);
              }
            })();
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      }
    }
  }
</script>

<style lang="scss" type="text/scss" scoped>
  @import '../../../styles/index.scss';
  .ticket{
    .item{
      margin-bottom: 10px;
    }
    span{
      font-weight: 500;
      margin-right: 10px;
      line-height: 38px;
    }
    .el-input{
      margin-top: 1px;
      width: 200px;
      margin-left: 20px;
      .span{
        margin-right: 10px;
        line-height: 40px;
      }
    }
  }
</style>
